<?php

namespace common\logics\stats;

use Yii;
use common\helpers\ArrayHelper;

/**
 * 时间统计
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
trait TimefilterTrait {

    /**
     * @var integer 店铺ID
     */
    public $storeid;

    /**
     * @var array 统计时间区间
     */
    public $datetime = [];

    /**
     * @var integer 开始时间
     */
    private $_begintime;

    /**
     * @var integer 结束时间
     */
    private $_endtime;

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['storeid'], 'integer'],
            [['datetime'], 'validateDatetime', 'skipOnEmpty' => false],
            [['datetime'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function validateDatetime($attribute) {
        $datetime = (array) $this->$attribute;
        $times = [];
        if (!empty($datetime)) {
            foreach ($datetime as $date) {
                $times[] = Yii::$app->formatter->asTimestamp($date);
            }
        }
        $times[0] = ArrayHelper::getValue($times, 0, strtotime('today'));
        $times[1] = ArrayHelper::getValue($times, 1, time());

        $this->_begintime = strtotime(date('Y-m-d', min($times)) . '00:00:00');
        $this->_endtime = strtotime(date('Y-m-d', max($times)) . '23:59:59');
    }

    /**
     * {@inheritdoc}
     */
    public function getBegintime() {
        return $this->_begintime ?: strtotime('today');
    }

    /**
     * {@inheritdoc}
     */
    public function getEndtime() {
        return $this->_endtime ?: strtotime('today') + 86400 - 1;
    }

    /**
     * {@inheritdoc}
     */
    public function getDateRange() {
        $begin = $this->begintime;
        $end = $this->endtime;
        $duration = abs($begin - $end);
        if ($duration <= 86400) {
            return date('Y年m月d日', $begin);
        }
        if (date('Ym', $begin) == date('Ym', $end)) {
            $month = date('Y年m月', $begin);
            $day1 = date('d日', $begin);
            $day2 = date('d日', $end);
            return $month . $day1 . '-' . $day2;
        }
        if (date('Y', $begin) == date('Y', $end)) {
            $year = date('Y年', $begin);
            $day1 = date('m月d日', $begin);
            $day2 = date('m月d日', $end);
            return $year . $day1 . '-' . $day2;
        }
        return date('Y年m月d日', $begin) . '-' . date('Y年m月d日', $end);
    }

    /**
     * {@inheritdoc}
     */
    public function getTimeInterval() {
        return [
            $this->_begintime,
            $this->_endtime,
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getCacheKeyName($name) {
        return implode('_', [
            get_called_class(),
            date('YmdH', $this->_begintime),
            date('YmdH', $this->_endtime),
            $name
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function getStoreId() {
        if ($this->storeid) {
            return $this->storeid;
        }
        if (!Yii::$app->user->isGuest) {
            return Yii::$app->user->identity->getStoreId();
        }
        return null;
    }

}
